package zombiedefense.effects;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.Random;
import zombiedefense.util.MathEx;
import zombiedefense.map.TileMap;

public class Spark extends Effect{
	
	private static final Color sparkColor = Color.white;
	private static final int numSparks = 5;
	private static final double maxSparkDist = .45;
	private static final double minSparkDist = .225;
	private static final double sparkVariance = Math.toRadians(20);
	
	private static double dist,dir;
	private static Random r;
	private Point2D.Double[] sparkPoints;;
	
	public Spark(){
	    //initialize the array
	    sparkPoints = new Point2D.Double[numSparks];
	    
	    for(int i = 0; i < sparkPoints.length; i++){
		sparkPoints[i] = new Point2D.Double();
	    }
	}
	
	public void setFields(double x,double y,long effectTime,double direction){
	    super.setFields(x, y, effectTime);
	    
	    r = MathEx.getRandom();
	    
	    for(int i = 0; i < sparkPoints.length; i++){
		
		dist = (r.nextDouble() * (maxSparkDist - minSparkDist)) + minSparkDist;
		dir = direction + (r.nextDouble() * sparkVariance * ((r.nextBoolean())?1:-1));
		
		sparkPoints[i].setLocation((dist * Math.cos(dir)) + x,
			(dist * Math.sin(dir)) + y);
	    }
	}
	
	public void draw(Graphics2D g,int drawx,int drawy,TileMap map){
	    g.setColor(sparkColor);
	    
	    //draw spark trails
	    for(Point2D.Double p: sparkPoints){
		g.drawLine(drawx,drawy,map.getScreenX(p.x),map.getScreenY(p.y));
	    }
	    
	}
    }